package ie.dcu.spca.data.db;

import java.util.List;

import ie.dcu.spca.data.UserDataManager;
import ie.dcu.spca.model.Bean;
import ie.dcu.spca.model.bean.User;
import ie.dcu.spca.model.dao.UserDao;

public class DBUserDataManager extends DBBasicDataManager implements
        UserDataManager {

    private static UserDao uDao = new UserDao();

    private List<User> usersCache = null;

    private UserDao userDao() {
        usersCache = null;
        return uDao;
    }

    public DBUserDataManager() {
        super();
    }

    @Override
    public int create(String name) {
        return userDao().insertUser(name);
    }

    @Override
    public List<User> get() {
        if (usersCache == null)
            usersCache = (List) userDao().selectAllBean();
        return usersCache;
    }

    @Override
    public void update(int id, String name) {
        User user = (User) userDao().selectById(id);
        user.setName(name);
        update(user);
    }

    @Override
    public void update(User user) {
        userDao().updateByBean(user);
    }

    @Override
    public void delete(int id) {
        userDao().deleteById(id);
    }

    @Override
    public List<Bean> getBeanList() {
        return (List) get();
    }

    @Override
    public int getId(Bean bean) {
        return ((User) bean).getId();
    }

    @Override
    public String getName(Bean bean) {
        return ((User) bean).getName();
    }

}
